﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace BeniBastanYaratProject
{
    public partial class Default : System.Web.UI.Page
    {
        DBBaglantiDataContext db = new DBBaglantiDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack) return;
            SonEklenenUrunleriDoldur();

        }

        private void SonEklenenUrunleriDoldur()
        {
            var sorgu = (from x in db.Urunlers select new { x.UrunID, x.UrunAdi,x.UrunResmiKucuk, UrunSatisFiyati = x.UrunSatisFiyati.ToString()+ " TL"}).Take(12);
            DataList1.DataSource = sorgu;
            DataList1.DataBind();
        }

        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "SepeteEkle")
            {
               
            if (Membership.GetUser() != null)
            {
                bool TransactionIsSuccessful = SepetIslemleri.SepeteEkle(Convert.ToInt32(e.CommandArgument), int.Parse("1"), new Guid(Membership.GetUser().ProviderUserKey.ToString()));
                if (TransactionIsSuccessful)
                {
                    ClientScript.RegisterStartupScript(typeof(Page), "Information", "alert(\"Urun Sepete Eklendi\");", true);
                    Master.SepetHesapla();
                   
                }
                else
                {
                    ClientScript.RegisterStartupScript(typeof(Page), "Error", "alert(\"Urun Sepete Ekleme Başarısız..\");", true);
                }
            }
            else
            {
                Response.Redirect("~/Login.aspx");
            } 
            }
        }
    }
}